import pandas as pd
#1 获取文本
f = open(r'1\test1.txt',"r")
str1=[word for word in f.read().split() if word]  # 使用split()自动处理多个空格，并过滤空字符串
print('分词结果',str1)
dic={}
for i in str1:
    if i not in dic:
        dic[i] = 1
    else:
        dic[i] += 1
#3 词频排序
#tuple1=zip(dic.values(),dic.keys())
#print(list(sorted(tuple1)))
list1= sorted(dic.items(),key=lambda x:x[1],reverse=True)
#4 输出结果
df=pd.DataFrame(list1,columns=['词语','次数'])
df.to_csv("test1.csv")
